import time
from datetime import datetime
from .base import TranscriptionObserver

class ConsoleObserver(TranscriptionObserver):
    """控制台输出观察者"""
    def __init__(self, show_timestamp: bool = True):
        self.show_timestamp = show_timestamp
        self.last_update_time = time.time()
        self.update_interval = 0.5

    def on_transcription_update(self, text: str, is_final: bool = False):
        current_time = time.time()
        if is_final or current_time - self.last_update_time >= self.update_interval:
            if self.show_timestamp:
                print(f"\r{datetime.now().strftime('%H:%M:%S')} | {text}", end="", flush=True)
            else:
                print(f"\r{text}", end="", flush=True)
            self.last_update_time = current_time 